﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class GeneralLedgerBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IGeneralLedgerDA generalLedgerDA = DataAccess.CreateGeneralLedgerDA();
        private static readonly IGeneralLedgerDetailDA generalLedgerDetailDA = DataAccess.CreateGeneralLedgerDetailDA();
        private PubParasInfo pubParasInfo;

        public GeneralLedgerBLL()
        {
            this.pubParasInfo = null;
        }

        public GeneralLedgerBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public GeneralLedgerBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        private void Copy(string strContractID, GeneralLedgerInfo generalLedgerInfo)
        {
            if ((generalLedgerInfo.GeneralLedgerDetailInfos != null) && (generalLedgerInfo.GeneralLedgerDetailInfos.Count > 0))
            {
                foreach (GeneralLedgerDetailInfo GLDI in generalLedgerInfo.GeneralLedgerDetailInfos)
                {
                    GLDI.GeneralLedgerID = strContractID;
                    GLDI.CreatedBy = this.pubParasInfo.UserID;
                    generalLedgerDetailDA.InsertGeneralLedgerDetail(GLDI, this.dataBaseID[0]);
                }
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<GeneralLedgerInfo> GeneralLedgerInfos = new List<GeneralLedgerInfo>();
                GeneralLedgerInfo subjectInfo = null;
                foreach (string id in IDS)
                {
                    subjectInfo = generalLedgerDA.GetGeneralLedgerByID(dataBase, tran, id, this.dataBaseID[0]);
                    GeneralLedgerInfos.Add(subjectInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                for (int n = 0; IDS.Count > n; n++)
                {
                    generalLedgerDetailDA.DeleteGeneralLedgerDetail(dataBase, tran, IDS[n], this.dataBaseID[0]);
                }
                if (generalLedgerDA.DeleteGeneralLedger(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<GeneralLedgerInfo>(dataBase, tran, GeneralLedgerInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "GENERAL_LEDGER", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return generalLedgerDA.GetAllGeneralLedger(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            GeneralLedgerInfo generalLedgerInfo = generalLedgerDA.GetGeneralLedgerByID(id, this.dataBaseID[0]);
            if (generalLedgerInfo != null)
            {
                generalLedgerInfo.GeneralLedgerDetailInfos = generalLedgerDetailDA.GetGeneralLedgerDetailByID(id, this.dataBaseID[0]);
            }
            return generalLedgerInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            GeneralLedgerQueryEntity generalLedgerQueryEntity = obj as GeneralLedgerQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(generalLedgerQueryEntity.GeneralLedgerID))
            {
                sqlWhere.Add(" GENERAL_LEDGER_ID " + generalLedgerQueryEntity.GeneralLedgerID);
            }
            if (!string.IsNullOrEmpty(generalLedgerQueryEntity.GeneralLedgerName))
            {
                sqlWhere.Add(" GENERAL_LEDGER_NAME " + generalLedgerQueryEntity.GeneralLedgerName);
            }
            if (!string.IsNullOrEmpty(generalLedgerQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + generalLedgerQueryEntity.IsActive);
            }
            return generalLedgerDA.GetGeneralLedgerByQueryList(sqlWhere, generalLedgerQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            GeneralLedgerInfo generalLedgerInfo = obj as GeneralLedgerInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(generalLedgerInfo.GeneralLedgerID))
                {
                    generalLedgerInfo.GeneralLedgerID = commonMethod.GenNextNumStr(dataBase, tran, "GENERAL_LEDGER", whLoginID);
                }
                generalLedgerInfo.UpdatedBy = generalLedgerInfo.CreatedBy;
                generalLedgerInfo.CreatedDate = DateTime.Now;
                generalLedgerInfo.UpdatedDate = DateTime.Now;
                if (generalLedgerDA.InsertGeneralLedger(dataBase, tran, generalLedgerInfo, whLoginID) > 0)
                {
                    strContractID = generalLedgerInfo.GeneralLedgerID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "GENERAL_LEDGER", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            this.Copy(strContractID, generalLedgerInfo);
            return strContractID;
        }

        public string Update(object obj)
        {
            GeneralLedgerInfo generalLedgerInfo = obj as GeneralLedgerInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                GeneralLedgerInfo oldGeneralLedgerInfo = generalLedgerDA.GetGeneralLedgerByID(dataBase, tran, generalLedgerInfo.GeneralLedgerID, this.dataBaseID[0]);
                if (generalLedgerDA.UpdateGeneralLedger(dataBase, tran, generalLedgerInfo, whLoginID) > 0)
                {
                    strContractID = generalLedgerInfo.GeneralLedgerID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldGeneralLedgerInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "GENERAL_LEDGER", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }

        public string UpdateGeneralLedger(GeneralLedgerInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                GeneralLedgerInfo oldGeneralLedgerInfo = generalLedgerDA.GetGeneralLedgerByID(dataBase, tran, obj.GeneralLedgerID, this.dataBaseID[0]);
                if (generalLedgerDA.UpdateGeneralLedger(dataBase, tran, obj, whLoginID) > 0)
                {
                    strContractID = obj.GeneralLedgerID;
                    new AuditDataBLL().InsertAuditData<GeneralLedgerInfo>(dataBase, tran, oldGeneralLedgerInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "GENERAL_LEDGER", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }
    }
}

